import React, { Component } from 'react';
import { Chart, Axis, Tooltip, Geom, Legend, Coord, Label } from 'bizcharts';
import DataSet from '@antv/data-set';
import Debounce from 'lodash-decorators/debounce';
import Bind from 'lodash-decorators/bind';
import autoHeight from '../autoHeight';
import styles from '../index.less';

@autoHeight()
class Bar extends Component {
  handleRoot = n => {
    this.root = n;
  };

  handleRef = n => {
    this.node = n;
  };

  @Bind()
  @Debounce(400)
  resize() {}
  render() {
    const {
      height,
      title,
      forceFit = true,
      data,
      color = 'rgba(24, 144, 255, 0.85)',
      padding,
    } = this.props;
    const ds = new DataSet();
    const dv = ds.createView().source(data);

    const scale = {
      月份: {
        type: 'cat',
      },
      月均降雨量: {
        min: 0,
      },
    };

    const tooltip = [
      'x*y',
      (x, y) => ({
        name: x,
        value: y,
      }),
    ];

    return (
      <div className={styles.chart} style={{ height }} ref={this.handleRoot}>
        <div ref={this.handleRef}>
          {title && <h4 style={{ marginBottom: 20 }}>{title}</h4>}
          <Chart
            scale={scale}
            height={title ? height - 41 : height}
            forceFit={forceFit}
            data={dv}
            padding={padding || 'auto'}
          >
            <Axis
              name="月份"
              label={{
                textStyle: {
                  fill: '#fff',
                },
              }}
            />
            <Axis
              name="月均降雨量"
              min={0}
              label={{
                textStyle: {
                  fill: '#fff',
                },
              }}
            />
            <Legend
              textStyle={{
                textAlign: 'start',
                fill: '#fff',
                fontSize: '14',
                fontWeight: 'bold',
              }}
              marker="square"
              position="right-top"
            />
            <Tooltip showTitle={false} crosshairs={{ type: 'rect' }} />
            <Geom
              color={['name', ['#FFC22D', '#1B9EFF', '#FF596F', '#1ACD7A']]}
              type="interval"
              position="月份*月均降雨量"
              adjust={[
                {
                  type: 'dodge',
                  marginRatio: 1 / 32,
                },
              ]}
            />
          </Chart>
        </div>
      </div>
    );
  }
}

export default Bar;
